﻿using System;
using System.Linq;
using RN.ERP.Web.PersonService;
using Newtonsoft.Json;
using System.Collections.Generic;
using RN.ERP.Web.ModelService;
using System.Text;
using System.Web;

namespace RN.ERP.Web.Exam
{
    public partial class BmFileDownload : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request["fileID"]))////下载文件操作
            {
                DownloadFile();
                return;
            }

            if (!IsPostBack)
            {
                if (IsUserValid())
                {
                    downloadArea.Visible = true;
                    FileListBind();
                }
                else
                {
                    msgArea.Visible = true;
                }
            }
        }

        public override bool IsCheckPermission
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 验证当前专家是否有下载文件的权限
        /// </summary>
        private bool IsUserValid()
        {
            using (PersonServiceClient psnClient = new PersonServiceClient())
            {
                int count = 0;
                var qc = JsonConvert.DeserializeObject<List<Exam_QC>>(psnClient.GetExamQCList
                       ("QC", UserInfo.RoleId, "BmQCList.aspx", SystemID, 0, 0, out count));
                var item = qc.OrderByDescending(q => q.EndDate).FirstOrDefault();

                if (item != null)
                {
                    string rstStr = psnClient.IsExamFeePaid(item.ID, UserInfo.CorpUserGuid.ToString());
                    OperationResult result = JsonConvert.DeserializeObject<OperationResult>(rstStr);
                    if (result.Result) return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        private void DownloadFile()
        {
            string fileID = Request["fileID"];

            if (!String.IsNullOrEmpty(fileID))
            {
                TestFile file = FileStoreManager.GetFile(fileID);
                TransmitFile(file.Path, file.Name + file.Suffix);
            }
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filePath">源路径</param>
        /// <param name="targetFileName">下载后名称</param>
        public void TransmitFile(string filePath, string targetFileName)
        {
            targetFileName = HttpUtility.UrlEncode(targetFileName, Encoding.UTF8);
            Response.ContentType = "application/x-zip-compressed";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + targetFileName);
            Response.HeaderEncoding = Encoding.UTF8;
            string filename = Server.MapPath(filePath);
            Response.TransmitFile(filename);
        }

        private void FileListBind()
        {
            List<TestFile> fileList = FileStoreManager.GetAllFile();
            rptFile.DataSource = fileList;
            rptFile.DataBind();
        }
    }
}